package cn.iocoder.yudao.module.agri.controller.admin.farmmodelstage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agri.controller.admin.farmmodelstage.vo.*;
import cn.iocoder.yudao.module.agri.dal.dataobject.farmmodelstage.FarmModelStageDO;
import cn.iocoder.yudao.module.agri.convert.farmmodelstage.FarmModelStageConvert;
import cn.iocoder.yudao.module.agri.service.farmmodelstage.FarmModelStageService;

@Tag(name = "管理后台 - 种养模型-生长阶段")
@RestController
@RequestMapping("/agri/farm-model-stage")
@Validated
public class FarmModelStageController {

    @Resource
    private FarmModelStageService farmModelStageService;

    @PostMapping("/create")
    @Operation(summary = "创建种养模型-生长阶段")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage:create')")
    public CommonResult<Long> createFarmModelStage(@Valid @RequestBody FarmModelStageCreateReqVO createReqVO) {
        return success(farmModelStageService.createFarmModelStage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新种养模型-生长阶段")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage:update')")
    public CommonResult<Boolean> updateFarmModelStage(@Valid @RequestBody FarmModelStageUpdateReqVO updateReqVO) {
        farmModelStageService.updateFarmModelStage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除种养模型-生长阶段")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage:delete')")
    public CommonResult<Boolean> deleteFarmModelStage(@RequestParam("id") Long id) {
        farmModelStageService.deleteFarmModelStage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得种养模型-生长阶段")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage:query')")
    public CommonResult<FarmModelStageRespVO> getFarmModelStage(@RequestParam("id") Long id) {
        FarmModelStageDO farmModelStage = farmModelStageService.getFarmModelStage(id);
        return success(FarmModelStageConvert.INSTANCE.convert(farmModelStage));
    }

    @GetMapping("/list")
    @Operation(summary = "获得种养模型-生长阶段列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage:query')")
    public CommonResult<List<FarmModelStageRespVO>> getFarmModelStageList(@RequestParam("ids") Collection<Long> ids) {
        List<FarmModelStageDO> list = farmModelStageService.getFarmModelStageList(ids);
        return success(FarmModelStageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得种养模型-生长阶段分页")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage:query')")
    public CommonResult<PageResult<FarmModelStageRespVO>> getFarmModelStagePage(@Valid FarmModelStagePageReqVO pageVO) {
        PageResult<FarmModelStageDO> pageResult = farmModelStageService.getFarmModelStagePage(pageVO);
        return success(FarmModelStageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出种养模型-生长阶段 Excel")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage:export')")
    @OperateLog(type = EXPORT)
    public void exportFarmModelStageExcel(@Valid FarmModelStageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FarmModelStageDO> list = farmModelStageService.getFarmModelStageList(exportReqVO);
        // 导出 Excel
        List<FarmModelStageExcelVO> datas = FarmModelStageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "种养模型-生长阶段.xls", "数据", FarmModelStageExcelVO.class, datas);
    }

}
